home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3579 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: Starbase.NeoSoft.COM!not-for-mail
  2. From: timd@Starbase.NeoSoft.COM (TimD)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: GCC and Default Constructors
  5. Date: 24 Jan 1996 17:10:52 -0600
  6. Organization: NeoSoft, Inc. +1 713 968 5800
  7. Message-ID: <4e6e9s$4so@Starbase.NeoSoft.COM>
  8. References: <4dtv71$1eh@news1.usa.pipeline.com> <1996Jan23.105906.1752@ittpub>
  9. NNTP-Posting-Host: starbase.neosoft.com
  10.  
  11. In article <1996Jan23.105906.1752@ittpub>, Wil Evers <wil@ittpub.nl> wrote:
  12. >In article <4dtv71$1eh@news1.usa.pipeline.com> grantp@usa.pipeline.com  
  13. >writes:
  14. >> On Jan 20, 1996 17:29:29 in article <GCC and Default Constructors>, 
  15. >> 'Eric Richard <erichard@netgen.com>' wrote: 
  16. >>   
  17. >> >I just spent about 3 days [...]
  18. >> > 
  19. >> >Basically my problem stemmed from a typo on my part and the fact 
  20. >> >that C++ will automatically provide a default constructor for you if 
  21. >> >you don't provide one.  [...]
  22. >> 
  23. >> >[...] what was screwing me up was that no 
  24. >> >error was being generated because G++ was happily providing me with 
  25. >> >a default constructor for my class. 
  26.  
  27. A very familiar problem.  And sometimes very annoying.  I think of
  28. this as an "unsafe" feature of C++.  It can result in direct memory
  29. copies where you don't want them.
  30.  
  31. >> >So, my question is this:  Does anybody know if there is some way 
  32. >> >of at least getting G++ to warn you if it is using a default 
  33. >> >constructor?  [...]
  34.  
  35. If there were, it would be right there on the man page.
  36.  
  37. >[...] as a  
  38. >matter of routine, I always consider what should happen if someone tries  
  39. >to copy an object of a class I wrote myself, and I make sure my classes  
  40. >either have a defined copy constructor and assignment operator (even when  
  41. >the compiler would have generated the same code), or I disable them by  
  42. >declaring them private.
  43.  
  44. Well, I'm not that paranoid--if a copy will work ok by direct memory
  45. copy or by calling copies of subclasses, I think it is okay.  But
  46. very often I write those function prototypes with no bodies in a
  47. private part of the class.  This means I will either get a compile
  48. or link error if they are used.
  49.  
  50. >Sometimes, when in a paranoid mood, I use the following helper class:
  51. >
  52. >    class NoGeneratedCopying {
  53. >    private :
  54. >        NoGeneratedCopying(const NoGeneratedCopying&);
  55. >        void operator=(const NoGeneratedCopying&);
  56. >    public :
  57. >        NoGeneratedCopying() { }
  58. >    };
  59. >
  60. >If you put a 'NoGeneratedCopying' data member in a class, the compiler is  
  61. >not allowed to generate a copy constructor or assignment operator for that  
  62. >class, and unless you explicitly provide them, an attempt to copy an  
  63. >object of that class will result in a compile-time error.
  64.  
  65. See, I didn't think of this!  But making it a data member seems 
  66. wrong from an intellectual perspective, I guess.  It seems more
  67. pleasing to write:
  68.  
  69.    class MyClass : public ItsParent, public NoGeneratedCopying
  70.    {
  71.       ...
  72.    }
  73.  
  74. That way I can say
  75.  
  76.    MyClass is-a NoGeneratedCopying object!
  77.  
  78. I don't think "virtual" is necessary 'cos there are no data members.
  79.  
  80. Just a thought!
  81.  
  82. -t
  83.